package com.nulldev.util.web.YouTubeExtractor.subtitles;

import java.util.ArrayList;
import java.util.List;

import com.nulldev.util.VariableAPI.StringsUtil;

public class Subtitle {
	private final String text;
	private final float start, dur, end;
	private volatile int positionV = -1, positionH = -1;
	private final boolean asMillis;
	private final List<SubtitleStyle> style = new ArrayList<SubtitleStyle>(0);

	public Subtitle(final String text, final float start, final float dur) {
		this(text, start, dur, false);
	}

	public Subtitle(final String text, final float start, final float dur, final boolean asMillis) {
		this(text, start, dur, null, asMillis);
	}

	public Subtitle(final String text, final float start, final float dur, final List<SubtitleStyle> styles, final boolean asMillis) {
		this.text = text;
		this.start = start;
		this.dur = dur;
		this.end = start + dur;
		this.asMillis = asMillis;
		if (styles != null && !styles.isEmpty())
			this.style.addAll(styles);
	}

	public String text() {
		return this.text;
	}

	public float start() {
		return this.start;
	}

	public float end() {
		return this.end;
	}

	public float duration() {
		return this.dur;
	}

	public int positionV() {
		return this.positionV;
	}

	public int positionH() {
		return this.positionH;
	}

	public Subtitle setPositionV(final int pos) {
		this.positionV = pos;
		return this;
	}

	public Subtitle setPositionH(final int pos) {
		this.positionH = pos;
		return this;
	}

	public List<SubtitleStyle> getStyles() {
		return this.style;
	}

	@Override
	public String toString() {
		return "Subtitle[start=" + this.start + ",duration=" + this.dur + ",end=" + this.end + ",text=" + this.text + "]";
	}

	public boolean hasPositionalData() {
		return this.positionV != -1 || this.positionH != -1;
	}

	public boolean treatAsMilliseconds() {
		return this.asMillis;
	}

	public static class SubtitleStyle {
		public String textIdf;
		public int color = -1;
		public int backgroundAlpha = -1, foregroundAlpha = -1;
		public float textSizeIncrement = -1;
		public int edgeColor = -1;
		public int fontFamily = -1;

		public boolean hasTextIdentifier() {
			return this.textIdf != null && !this.textIdf.isEmpty();
		}

		public boolean hasColor() {
			return this.color > -1;
		}

		public boolean hasTextSizeIncrement() {
			return this.textSizeIncrement > -1 && this.textSizeIncrement <= 100;
		}

		public boolean hasBackgroundAlpha() {
			return this.backgroundAlpha > -1 && this.backgroundAlpha < 255;
		}

		public boolean hasForegroundAlpha() {
			return this.foregroundAlpha > -1 && this.foregroundAlpha < 255;
		}

		public boolean hasEdgeColor() {
			return this.edgeColor > -1;
		}

		public boolean hasFontFamily() {
			return this.fontFamily > -1 && this.fontFamily <= 7;
		}

		public SubtitleStyle() {
			this.textIdf = StringsUtil.randomStringAlphabet(12);
		}

		public boolean qualifies() {
			return this.hasTextIdentifier() && (this.hasColor() || this.hasBackgroundAlpha() || this.hasTextSizeIncrement() || this.hasForegroundAlpha()
					|| this.hasEdgeColor() || this.hasFontFamily());
		}

		public static String getFontFamilyForInt(final int v) {
			switch (v) {
				case 1:
					return "\"Courier New\", Courier, \"Nimbus Mono L\", \"Cutive Mono\", monospace";
				case 2:
					return "\"Times New Roman\", Times, Georgia, Cambria, \"PT Serif Caption\", serif";
				case 3:
					return "\"Deja Vu Sans Mono\", \"Lucida Console\", Monaco, Consolas, \"PT Mono\", monospace";
				case 5:
					return "\"Comic Sans MS\", Impact, Handlee, fantasy";
				case 6:
					return "\"Monotype Corsiva\", \"URW Chancery L\", \"Apple Chancery\", \"Dancing Script\", cursive";
				case 7:
					return "\"Carrois Gothic SC\", \"Arial Unicode MS\", Arial, Helvetica, Verdana, \"Marcellus SC\", sans-serif-smallcaps, sans-serif";
				case 0:
				case 4:
				default:
					return "\"YouTube Noto\", Roboto, \"Arial Unicode MS\", Arial, Helvetica, Verdana, \"PT Sans Caption\", sans-serif";
			}
		}
	}

	public boolean hasStyles() {
		return !this.style.isEmpty();
	}

	public int getQualifiyingStyles() {
		int q = 0;
		for (final SubtitleStyle s : this.style) {
			if (s.qualifies())
				q++;
		}
		return q;
	}
}
